package ModelLayer;
import java.util.ArrayList;

/**
 * The class AddressBook holds all of our persons in a collection.
 * 
 * @author (your name) 
 * @version (a version number or a date)
 */

public class AddressBook
{
    private ArrayList<Person> addressBook;
    private Person person;
    private static AddressBook instance;

        /**
     * Constructor for objects of class AddressBook
     * creates an array that stores persons
     */
    private AddressBook()
    {
        addressBook = new ArrayList<Person>();
    }

    /**
     * singleton instance
     */
    public static AddressBook getInstance()
    {
        if(instance == null)
        {
            instance = new AddressBook();
        }
        return instance;
    }

     /**
     * Adding a Person to the colletion
     * 
     * @param newPerson - the objectname of the Person
     */
    public void addPerson(Person newPerson)
    {
        addressBook.add(newPerson);

    }

    /**
     * returns a Person searched by its idnumber
     * @param searchId - the id of the Person
     */
    public Person findPersonId(int searchId)
    {
        int index = 0;
        boolean found= false;
        int bigIndex = 0;
        while(index < addressBook.size() && !found)
        {
            Person person2 = addressBook.get(index);
            if(person2.getId() == searchId)
            {
                found = true;
                bigIndex = index;
            }
            else
            {  
                index++;
            }
        }

        if(found == true)
        {
            Person person = addressBook.get(bigIndex);
            return person;
        }

        else
        {
            return null;
        }

    }

     /**
     * returns a Person searched by its name
     * @param searchName - the name of the Person
     */
    public Person findPersonName(String searchName)
    {
        int index = 0;
        boolean found= false;
        int bigIndex = 0;
        while(index < addressBook.size() && !found)
        {
            Person person2 = addressBook.get(index);
            if(person2.getName().equals(searchName))
            {
                found = true;
                bigIndex = index;
            }
            else
            {  
                index++;
            }
        }

        if(found == true)
        {
            Person person = addressBook.get(bigIndex);
            return person;
        }

        else
        {
            return null;
        }
    }

    /**
     * deletes a person found by its idnumber
     * @param searchId - the id of the Person
     */
    public void removePerson(int searchId)

    {
        Person person2 = findPersonId(searchId);
        addressBook.remove(person2);

    }

     /**
     * Update a Person
     * 
     * @param searchId - get the DVD by the id
     * @param id - update the id
     * @param name - update the name
     * @param address - update the address
     * @param zipCode - update the zipCode
     * @param city - update the city
     * @param phoneNmber - update the phoneNumber
     */
    public void changePerson(int searchId, int id, String name, String address, String zipCode, String city, String phoneNumber)
    {
        Person person2 = findPersonId(searchId);
        person2.setId(id);
        person2.setName(name);
        person2.setAddress(address);
        person2.setZipCode(zipCode);
        person2.setCity(city);
        person2.setPhoneNumber(phoneNumber);
    }

    /**
     * shows information about a person by its idnumber
     * @param searchId - the id of the Person
     */
    public void showPersonInfo(int searchId)
    {
        Person person2 = findPersonId(searchId);
        System.out.println("ID: " + person2.getId());
        System.out.println("Name: " + person2.getName());
        System.out.println("Address: " + person2.getAddress());
        System.out.println("ZipCode: " + person2.getZipCode());
        System.out.println("City: " + person2.getCity());
        System.out.println("Phonenumber: " + person2.getPhoneNumber());
    }

    /**
     * shows information about all persons by its idnumber
     */
    public void listPersonInfo()
    {
        for(Person person2 : addressBook)

        {
            System.out.println("ID: " + person2.getId());
            System.out.println("Name: " + person2.getName());
            System.out.println("Address: " + person2.getAddress());
            System.out.println("ZipCode: " + person2.getZipCode());
            System.out.println("City: " + person2.getCity());
            System.out.println("Phonenumber: " + person2.getPhoneNumber());
            System.out.println("********");

        }

    }
    
}

